package com.workpaper.app.init;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.workpaper.app.cache.config.ConfigCache;
import com.workpaper.app.utils.resource.PathUtils;
import com.workpaper.app.utils.resource.ResourceUtils;

/**
 * 配置初始化,加载配置项，如数据源配置信息
 * @author zyj
 *
 */
public final class ConfigInitor {
	/**
	 * 加载所有配置
	 */
	public static final void loadConfig() {
		loadDSConfig();
	}
	
	/**
	 * 加载数据源配置信息
	 */
	private static void loadDSConfig() {
		String dsConfigPath = PathUtils.getAppClassesPath() + "config/datasource_config.properties";
		loadProperties(dsConfigPath);
	}
	
	
	
	private static void loadProperties(String propertiesPath) {
		ResourceBundle resource = null;
		InputStream in = null;
		try {
			in = new FileInputStream(propertiesPath);
			resource = new PropertyResourceBundle(in);
			Enumeration<String> keys = resource.getKeys();
			while(keys.hasMoreElements()) {
				String key = keys.nextElement();
				ConfigCache.push(key, resource.getString(key));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ResourceUtils.closeResource(in);
			in = null;
		}
		
		
	}
}
